package bdz.projects.cbt.core
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;

	public class CBTInventory extends EventDispatcher
	{
		private var _items:Dictionary;
		private var _capacity:Number;
		public function CBTInventory(cap:Number){
			super();
			_capacity = cap;
			_items = new Dictionary();
		}
		public function addProduct(p:CBTProduct):void{
			if(openSlots > 0){
				if(_items[p.name] == null){
					_items[p.name] = new ProductCollection(p.name);
				}
				(_items[p.name] as ProductCollection).addProduct(p);
			}
			
		}
		public function removeAllProducts(s:String):ProductCollection{
			var retref:ProductCollection = _items[s] as ProductCollection;
			_items[s] = null;
			return retref;	
		}
		public function removeSomeProduct(s:String, count:Number):void{
			var remref:ProductCollection = _items[s] as ProductCollection;
			remref.splice(0, count);
		}
		public function get itemCount():Number{
			var count:Number = 0;
			for (var prop:String in _items){
				
				try{
					count += (_items[prop] as ProductCollection).length;
				}catch(e:Error){}
			}
			return count;
		}
		public function get productList():Array{
			return items;
		}
		public function get items():Array{
			var retarr:Array = new Array();
			for(var prop:String in _items){
				if(_items[prop]){
					retarr.push(_items[prop] as ProductCollection);
				}
			}
			return retarr;
		}
		public function get totalValue():Number{
			var tv:Number = 0;
			for (var prop:String in _items){
				try{
					tv += (_items[prop] as ProductCollection).totalPrice;
				}catch(e:Error){
					
				}
			}
			return tv;
		}
		public function upgradeCapacity(n:Number):void{
			_capacity += n;
		}
		public function get capacity():Number{
			return _capacity;
		}
		public function get openSlots():Number{
			return _capacity - itemCount;
		}
		
	}
}